package cn.itsource.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.itsource.annotation.LogAOP;
import cn.itsource.domain.User;
import cn.itsource.dto.ModifyPasswordDto;
import cn.itsource.exception.BusinessException;
import cn.itsource.mapper.UserMapper;
import cn.itsource.query.Query;
import cn.itsource.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    //查询所有
    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }

    //查询一条数据
    @Override
    public User findOne(Long id) {
        return userMapper.findOne(id);
    }

    //添加
    @Transactional
    @Override
    public void add(User user) {
        //使用hutool工具包生产一个随机的盐值
        String salt = RandomUtil.randomString(32);
        //使用md5给密码加密
        String passwordmd5 = SecureUtil.md5(user.getPassword() + salt);
        //重新设置数据
        user.setSalt(salt);
        user.setPassword(passwordmd5);//加密加盐之后的密码
        userMapper.add(user);
    }

    //根据id修数据
    @Transactional
    @Override
    public void update(User user) {
        userMapper.update(user);
    }

    //根据id修改
    @Transactional
    @Override
    @LogAOP
    public void delete(Long id) {
        userMapper.delete(id);
    }

    //分页+模糊查询
    @Override
    public PageInfo<User> pageQuery(Query query) {
        PageHelper.startPage(query.getCurrentPage(), query.getPageSize());
        List<User> list = userMapper.pageQuery(query);
        return new PageInfo<>(list);
    }

    //批量删除
    @Transactional
    @Override
    @LogAOP
    public void patchdel(Long[] ids) {
        userMapper.patchdel(ids);

    }

    @Override
    public User tologin(Map<String, String> map) {
        //获取账号，密码
        String account = map.get("account");
        String checkPass = map.get("checkPass");

        //校验账号或者密码是否为空  使用hutool工具包StrUtil.isBlank()方法
        if (StrUtil.isBlank(account) || StrUtil.isBlank(checkPass)){
            throw  new BusinessException("账号或者密码不能为空");
        }
        //校验账号是否存在
        User user = userMapper.findAccount(account);
        if (StrUtil.isBlankIfStr(user)){ //判断查询的数据是否为空
            throw new BusinessException("请输入的账号或邮箱不存在");
        }
        //校验密码是否正确
        String md5psw = SecureUtil.md5(checkPass + user.getSalt());
        if (!md5psw.equals(user.getPassword())){
            throw new BusinessException("你输入的密码错误");
        }
        return user;
    }

    //修改密码
    @Transactional
    @Override
    public void modifyPassword(ModifyPasswordDto modifyPasswordDto, User user) {
        String oldpassword = modifyPasswordDto.getOldpassword();
        String password = modifyPasswordDto.getPassword();
        String password2 = modifyPasswordDto.getPassword2();
        //密码修改不能为空。
        if (StrUtil.isBlank(oldpassword) || StrUtil.isBlank(password) ||StrUtil.isBlank(password2)){
            throw  new BusinessException("密码修改不能为空");
        }

        //校验用户输入的旧密码和登录密码是否相同，
        String md5Oldpsw = SecureUtil.md5(oldpassword + user.getSalt());
        if (!md5Oldpsw.equals(user.getPassword())){
            throw new BusinessException("你输入的原密码错误");
        }

        //校验修改的密码两次是否正确
        if (!password.equals(password2)){
            throw new BusinessException("你输入的修改密码错误");
        }

        //修改密码
        String randomString = RandomUtil.randomString(32); //盐值
        String md5 = SecureUtil.md5(password2 + randomString);
        user.setSalt(randomString);
        user.setPassword(md5);
        userMapper.update(user);
    }




    /*public static void main(String[] args) {
        String randomString = RandomUtil.randomString(32);
        String password = "123456";
        String md5 = SecureUtil.md5(password + randomString);
        System.out.println(randomString);
        System.out.println(md5);
    }*/

}
